#include "stdafx.h"
#include "ThreadPool.h"

ThreadPool::ThreadPool()
{

}

ThreadPool::~ThreadPool()
{
    for(ThreadListIter it = m_lBusyThread.begin(); it != m_lBusyThread.end(); it++)
    {
        delete *it;
        *it = NULL;
    }

    for(ThreadListIter it = m_lBusyThread.begin(); it != m_lBusyThread.end(); it++)
    {
        delete *it;
        *it = NULL;
    }
}

WorkThread* ThreadPool::GetThread()
{
    if(m_lIdleThread.size() == 0)
    {
        WorkThread* pThread = new WorkThread;
        m_lBusyThread.push_back(pThread);
        return pThread;
    }
    else
    {
        WorkThread *pThread = *(m_lIdleThread.begin());
        m_lIdleThread.erase(m_lIdleThread.begin());
        m_lBusyThread.push_back(pThread);

        return pThread;
    }
}

void ThreadPool::ReleaseThread(WorkThread* pThread)
{
    ThreadListIter it = m_lBusyThread.begin();
    for(; it != m_lBusyThread.end(); it++)
    {
        if(*it == pThread )
        {
            m_lBusyThread.erase(it);
        }
    }

    m_lIdleThread.push_back(pThread);
}

void ThreadPool::GetMatchIDList( std::vector<DWORD>& vecMatchID )
{
    ThreadListIter iter = m_lBusyThread.begin();
    for(; iter != m_lBusyThread.end(); iter++)
    {
        vecMatchID.push_back((*iter)->GetMatchID());
    }

}

